quietly{
clear

local filename "${name}_est_`1'`3'"

* Parameters passed into do file:
* `1': LRR for estimation (F, G, H, K, L, M, or N)
* `2': Subset of fields to estimate, either acres<10 or acres>=10
* `3': Name of field subset, either "small" or "large"

* Define possible crop outcomes to loop over
* 1 = corn, 5 = soybeans, 9 = other
local states 1 5 9

* Load data
use oid crop z2 z2cf crop1Prev t lrrsym sameCrop acres cntyfips statefips region if lrrsym=="`1'" & `2' & sameCrop==0 using $datafile
* Need to drop a few regions in G that only have a few observations in order to estimate standard errors. Only drops 12 fields.
drop if lrr=="G" & (inlist(region,52,88,94))

* Estimate multinomial logit
mlogit crop z2 ib9.crop1Prev i.t i.region, vce(cluster region) base(9)
estimates save "${data}/`filename'", replace

* Save estimates
** Save stripped-down table to use in Matlab procedure
esttab using "${data}/`filename'.csv", not nopar nostar
** Save formatted table that can be easily copied into Word table
esttab using "${tables}/`filename'.csv", se nostar pr2

* Save variance-covariance matrix of estimates
matrix V=get(VCE)
matrix list V
putexcel set "${data}/vce_`filename'", replace
putexcel A1 = matrix(V), names
putexcel close

}
disp "done at `c(current_time)'"